//
//  SlideMenuController.swift
//  AlterationSwitch
//
//  Created by solarke_apple_mini_1 on 2018/10/24.
//  Copyright © 2018年 石家庄科林电气股份有限公司. All rights reserved.
//

import UIKit
import SnapKit

class SlideMenuController: UIViewController {
    // MARK: - IBOutlets
    @IBOutlet weak var menuContainer: UIView!
    @IBOutlet weak var contentContainer: UIView!
    
    // MARK: - Properties
    var maskButton: UIButton!
    var folded: Bool = true
    var originalPoint: CGPoint!
    let scaleFactor: CGFloat = 1
    
    lazy var offsetX: CGFloat = {
        return (self.view.frame.size.width / 2 + 100)
    }()
    
    var originalX: CGFloat {
        return (folded ? 0 : offsetX)
    }
    
    lazy var deltaScaleFactor: CGFloat = {
        return (1 - self.scaleFactor) / self.offsetX
    }()
    
    lazy var deltaAlphaFactor: CGFloat = {
        return 1 / self.offsetX
    }()
    
    lazy var miniumOffsetX: CGFloat = {
        return (self.offsetX / 2)
    }()
    
    // MARK: - Function
    func showMenuViewController() {
        // 显示边框的阴影效果
        setShadowVisible(visible: true)
        
        // 执行动画效果
        var transform = CGAffineTransform(translationX: offsetX, y: 0)
        transform = transform.scaledBy(x: scaleFactor, y: scaleFactor)
        
        UIView.animate(withDuration: 0.35,
                       animations: { () -> Void in
                        self.menuContainer.alpha = 1
                        self.contentContainer.transform = transform
        },
                       completion: { (Bool) -> Void in
                        self.setMaskButtonVisible(visible: true)
        })
    }
    
    func showContentViewController() {
        // 隐藏边框的阴影效果
        setShadowVisible(visible: true)
        
        // 执行动画效果
        UIView.animate(withDuration: 0.35,
                       animations: { () -> Void in
                        self.menuContainer.alpha = 0
                        self.contentContainer.transform = .identity
        },
                       completion: { (Bool) -> Void in
                        self.setMaskButtonVisible(visible: false)
        })
    }
    
    func switchController() {
        folded = !folded
        if folded {
            showContentViewController()
        } else {
            showMenuViewController()
        }
    }
    
    // MARK: - Internal
    func setShadowVisible(visible: Bool) {
        if visible {
            let path = UIBezierPath(rect: contentContainer.layer.bounds)
            contentContainer.layer.shadowPath = path.cgPath
            contentContainer.layer.shadowColor = UIColor.black.cgColor
            contentContainer.layer.shadowOffset = .zero
            contentContainer.layer.shadowOpacity = 0.4
            contentContainer.layer.shadowRadius = 4.5
        } else {
            contentContainer.layer.shadowRadius = 0
        }
    }
    
    func setMaskButtonVisible(visible: Bool) {
        if maskButton == nil {
            // 创建按钮
            maskButton = UIButton(type: .custom)
            maskButton.addTarget(self, action: #selector(SlideMenuController.maskButtonPressed), for: .touchUpInside)
            
            // 设置约束
            contentContainer.addSubview(maskButton)
            maskButton.snp.makeConstraints { (make) -> Void in
                make.edges.equalTo(contentContainer).inset(UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0))
            }
        }
        maskButton.isHidden = !visible
    }
    
    @objc func maskButtonPressed() {
        folded = !folded
        showContentViewController()
    }
}
